<?php
// +-------------------------------------------------------------------
// |
// +-------------------------------------------------------------------
// | Copyright (c) 2009-2016 All rights reserved.
// +-------------------------------------------------------------------
namespace Service\Api\V1;

use Think\Model;

class HouseContractDetail
{
    public $login = false; //是否需要登录才能访问该接口
    public $title = "租赁合同详情ok";
    public $group = '用户';
    public $desc = "";

    public $input = [
        'id' => 'required;int;label=租赁合同id;',
    ];

    public $output = [
        [
            'id' => 'label=id;',
            'code' => 'label=合同编号;',
            'house_id' => 'label=房间id;',
            'building_id' => 'label=楼栋id;',
            'building_name' => 'label=楼栋号;',
            'house_name' => 'label=房间号;',
            'area' => 'label=房间面积;',
            'sign_time' => 'label=合同签订时间;',
            'start_time' => 'label=租赁开始时间;',
            'end_time' => 'label=租赁结束时间;',
            'is_decorate' => 'label=是否在装修： 0否 1是;',
            'decorate_start_time' => 'label=装修开始时间;',
            'decorate_end_time' => 'label=装修结束时间;',
            'deposit' => 'label=押金;',
            'decorate_deposit' => 'label=装修押金;',
            'rent_price' => 'label=房租标准(元/平米/月);',
            'service_type' => 'label=物业费标准: 0标准价 1内部优惠价 2高新企业优惠价;',
            'service_price' => 'label=物业费(元/月);',
            'water_price' => 'label=水费标准(元/吨);',
            'elec_price' => 'label=电费标准(元/度);',
            'remark' => 'label=备注;',
            'file' => 'label=附件 多个使用英文逗号分隔;',
            'state' => 'label=状态： 0退租 1在租;',
            'service_start_time' => 'label=物业费缴费开始时间;',
            'service_end_time' => 'label=物业费缴费到期时间;',
            'f_moth' => 'label=房租预缴月数;',
            'w_moth' => 'label=物业费预缴月数;',
            'is_hide_refund' => 'label=是否显示退租申请按钮;comment=0隐藏1显示',
            'deposit_money' => 'label=保证金金额;',
            'rent_price2' => 'label=租金实价（元/月）;',
        ]
    ];

    public function run($param, $uid)
    {
        $model = new Model();
        $sql = "SELECT a.*, c.`name` AS building_name, d.`name` AS house_name FROM `k_house_contract` as a
LEFT JOIN k_building AS c ON c.id=a.building_id
LEFT JOIN k_house AS d ON d.id=a.house_id WHERE  a.id= {$param['id']};";
        $list = $model->query($sql);
        $list[0]['rent_price'] = number_format($list[0]['rent_price2'], 2);
        if(!$list) throw new \Exception('未找到该合同');

        if($list[0]['state'] != 0){
            $housse_refund = M('refund_house')->where([
                'contact_code' => $list[0]['code'],
                'uid' => $uid,
                'state' => ['in', '1,3'],
            ])->find();
            if(isset($housse_refund['id'])) {
                $list[0]['is_hide_refund'] = 0;
            }else{
                $list[0]['is_hide_refund'] = 1;
            }
        }


        return $list[0];
    }
}
